/**
 * Created by chenyi on 2017/12/20.
 */



$(document).ready(function () {
    //获取所有菜单
    var menus=$(".layui-layout-left").find('a[menu-url]');
    for(var i=menus.length-1;i>=0;i--){
        //默认显示菜单
        createMenu($(menus[i]).attr("menu-url")+"?_" + $.now(),$(menus[i]).attr("menu-id"));
    }

});
function renderMenu(menuId){
    $(".menu-li").hide();
    $("#LAY-system-side-menu").find("#"+menuId).show();
}
//生成菜单
function createMenu(url,menuId) {
    // $(".layui-nav-tree").html("");
    var _display="admin"==menuId?"block":"none";
    $.getJSON(url, function (r) {
        $("#LAY-system-side-menu").append('<div id="'+menuId+'" class="menu-li" style="display: '+_display+'"></div>');
        for (var i = 0; i < r.menuList.length; i++) {
            var _li;
            if (r.menuList[i].type === 0) {
                _li = ['<li data-name="'+r.menuList[i].url+'" class="layui-nav-item" >',
                    '<a href="javascript:;" lay-tips="'+r.menuList[i].name+'" lay-direction="2" >',
                    '<i class="' + r.menuList[i].icon + '"></i>',
                    '<cite>'+r.menuList[i].name+'</cite>',
                    '</a></li>'].join("");
                //是否有下级菜单
                if (r.menuList[i].list) {
                    var $li = $(_li);
                    $li.find("a").after('  <dl class="layui-nav-child"></dl>');
                    var childNodes = addMenu(r.menuList[i].list);
                    if (childNodes != "") {
                        $li.find(".layui-nav-child").append(childNodes);
                        _li = $li.prop("outerHTML");
                    }
                }
            }
            if (r.menuList[i].type === 1) {
                _li = $('<li  data-name="' + r.menuList[i].url + '" class="layui-nav-item"><a href="javascript:;" lay-href="' + r.menuList[i].url + '" lay-tips="' + r.menuList[i].name + '" lay-direction="2">' +
                    '<i class="' + r.menuList[i].icon + '"></i> <cite>' + r.menuList[i].name + '</cite></a></li>');
            }

            $("#"+menuId).append(_li);

        }

        layui.use('element', function () {
            var element = layui.element;
            element.render();

        });

        var menuNames = $("#LAY-system-side-menu").find("a");
        $.each(menuNames,function (index,item) {
            var $this = $(item);
            $this.hover(function () {
                layer.tips($this.text(),$this);
            })
        })

    });

}
//递归显示菜单 支持多级
function addMenu(list) {
    if (list) {
        var lis = "";
        for (var i = 0; i < list.length; i++) {
            var _li;
            if (list[i].type === 0) {
                _li = ['<dd data-name="' + list[i].url + '"><a href="javascript:;" ><i class="' + list[i].icon + '"></i>' + list[i].name + '</a></dd>'].join("");
                //是否有下级菜单
                if (list[i].list) {
                    var $li = $(_li);
                    $li.find("a").after('<dl class="layui-nav-child"></dl>');
                    var childNodes = addMenu(list[i].list);
                    if (childNodes != "") {
                        $li.find(".layui-nav-child").append(childNodes);
                    }
                }
                _li = $li.prop("outerHTML");
            }
            if (list[i].type === 1) {

                _li = $('<dd data-name="' + list[i].url + '"> <a href="javascript:;"  lay-href="' + list[i].url + '"><i class="' +  list[i].icon + '"></i>' + list[i].name + '</a> </dd>');
            }
            lis += $(_li).prop("outerHTML");
        }
        return lis;
    }
}


$(window).on('resize', function () {
    var $content = $('.content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function () {
        $(this).height($content.height());
    });

}).resize();

function clearStorage() {
    localStorage.clear();
    var  index=parent.layer.msg("清除中...", {
        icon: 16
        ,shade: false
        ,time:0
    });
    setTimeout(function () {
        layer.close(index);
        layer.msg('清除成功 !', {icon: 1});
    }, 2000);

}



//公告
function notice() {
    layer.open({
        type: 1
        ,
        title: false //不显示标题栏
        ,
        closeBtn: false
        ,
        area: '600px;'
        ,
        shade: 0.8
        ,
        id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,
        resize: false
        ,
        btn: ['打赏作者', '残忍拒绝']
        ,
        btnAlign: 'c'
        ,
        moveType: 1 //拖拽模式，0或者1
        ,
        content: ['<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'
            , '1.为防止网友误删数据,已启用测试账号<br>&nbsp;&nbsp;&nbsp;如需管理员账号请加群<span style="color:#2991D9;" >275846351 &nbsp;&nbsp;&nbsp;<a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=5b2ec31ee55abc44722cf8b2c7f7807d5b44d9a08da06de2c589c305e4742364"><img border="0" src="//pub.idqqimg.com/wpa/images/group.png" alt="cy-security" title="cy-security"></a></span><br><br>'
            , '2.开源不易,如有帮助到你,请作者喝杯咖啡吧<br><br>'
            , '3.打赏50元以上送<span style="color:#2991D9;" >定制视频</span>一份或<span style="color:#2991D9;" >html版前端框架 </span><br>'
            , '&nbsp;&nbsp;&nbsp;(土豆作品地址<a href="http://id.tudou.com/i/UMTQ5MTY4MzM2MA" target="_blank" style="color:#2991D9;" >http://id.tudou.com/i/UMTQ5MTY4MzM2MA</a>)</div>'].join("")
        ,
        success: function (layero) {
            var btn = layero.find('.layui-layer-btn');
            btn.find('.layui-layer-btn0').attr({
                href: 'javascript:reward();'
            });
        }
    });
}




$(document).ready(function () {
    $.getJSON("sys/user/info?_" + $.now(), function (r) {
        $("#user-pic").find("cite").html(r.user.username);
        $("#user-pic").find("img").attr("src",$t.getImgBaseUrl() + r.user.headPic);
        localStorage.setItem("headPic", $t.getImgBaseUrl() + r.user.headPic);
        localStorage.setItem("username", r.user.username);
        localStorage.setItem("userId", r.user.userId);
        $(".message-count").text(r.messageCount);
    });
});